package com.zhl.study;

import com.zhl.study.entity.MyBot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import java.net.InetAddress;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.net.UnknownHostException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class TelegramBotApplication {
    private static final Logger logger = LoggerFactory.getLogger(TelegramBotApplication.class);

    public static void main(String[] args) throws UnknownHostException {
        /**getUpdates方式，已测试通过*/
        try {
            TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
            DefaultBotOptions botOptions = new DefaultBotOptions();
            botOptions.setProxyHost("127.0.0.1");
            botOptions.setProxyPort(7890);
            // Select proxy type: [HTTP|SOCKS4|SOCKS5] (default: NO_PROXY)
            botOptions.setProxyType(DefaultBotOptions.ProxyType.HTTP);
            MyBot myBot = new MyBot(botOptions);
            telegramBotsApi.registerBot(myBot);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
        /**getUpdates方式，已测试通过*/

        /**WebHook方式，未测试*/
        /**  Webhook方式
        try {
            TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
            telegramBotsApi.registerBot(new MyBot());

			Webhook webhook = new DefaultWebhook();
			//Webhooks_目前支持的端口：443, 80, 88, 8443
			webhook.setInternalUrl("http://0.0.0.0:8443");
			TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class, webhook);
			MyWebHookBot myWebHookBot = new MyWebHookBot();
			//需要一个可以https访问的域名并转发到上面端口
			SetWebhook setWebhook = SetWebhook.builder().url("https://joelwublog.com/").build();
			telegramBotsApi.registerBot(myWebHookBot, setWebhook);

        } catch (TelegramApiException e) {
            e.printStackTrace();
        }*/
        /**WebHook方式，未测试*/
        ConfigurableApplicationContext application = SpringApplication.run(TelegramBotApplication.class, args);

        Environment env = application.getEnvironment();
        logger.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\t http://localhost:{}\n\t" +
                        "External: \t http://{}:{}\n\t" +
                        "Doc: \t http://{}:{}/doc.html\n" +
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                env.getProperty("server.port") + env.getProperty("server.servlet.context-path"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port") + env.getProperty("server.servlet.context-path"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port") + env.getProperty("server.servlet.context-path"));
    }
}